package com.tianji.promotion.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 自定义线程池
 */
@Slf4j
@Configuration
public class PromotionConfig {
    @Bean
     public Executor generateExchangeCodeExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //设置线程池大小
        executor.setCorePoolSize(2);
        //最大线程数
        executor.setMaxPoolSize(5);
        //设置队列大小
        executor.setQueueCapacity(200);
        //设置线程名
        executor.setThreadNamePrefix("exchange-code-handler-");
        //拒绝策略(设置谁提交线程谁执行)
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

}
